﻿'******************************************************
'Program Name: OCDSCore
'
'Version:      2.1
'
'Class Name:   OCDSCore
'
'Copyright:    Copyright 2011 MeaMod Group. All Rights Reserved.
'******************************************************
Imports Declarations
Imports Declarations.Players
Imports Declarations.Media
Imports Declarations.Events
Imports Implementation

Public Class OCDSCore
    Private m_factory As IMediaPlayerFactory
    Private m_player As IDiskPlayer
    Private m_media As IMedia
    Private _mediaPath As String
    Private _mediaStateChange As String
    Private _playercontrol As ucControls
    Private _mediaTag As TagLib.File = Nothing

    Public Property WindowHandle As IntPtr
        Get
            Return m_player.WindowHandle
        End Get
        Set (value As IntPtr)
            m_player.WindowHandle = value
        End Set
    End Property

    Public Property Volume As Integer
        Get
            Return m_player.Volume
        End Get
        Set (value As Integer)
            m_player.Volume = value
        End Set
    End Property

    Public Property Position As Single
        Get
            Return m_player.Position
        End Get
        Set (value As Single)
            m_player.Position = value
        End Set
    End Property

    Public Property Time As Long
        Get
            Return m_player.Time
        End Get
        Set (value As Long)
            m_player.Time = value
        End Set
    End Property

    Public Property MediaPath As String
        Get
            Return _mediaPath
        End Get
        Set(value As String)
            _mediaPath = value
            TagOpen()
        End Set
    End Property

    Public Property Mute As Boolean
        Get
            Return m_player.Mute
        End Get
        Set (value As Boolean)
            m_player.Mute = value
        End Set
    End Property

    Public ReadOnly Property CanPause As Boolean
        Get
            Return m_player.CanPause
        End Get
    End Property

    Public Property Playercontrol As ucControls
        Get
            Return _playercontrol
        End Get
        Set (value As ucControls)
            _playercontrol = value
        End Set
    End Property

    Public Sub Init (WindowH As IntPtr, PControl As ucControls)
        m_factory = New MediaPlayerFactory()
        'm_media.AddOptions({"-I", "dumy", "--ignore-config", "--no-osd", "--disable-screensaver", "--plugin-path=./plugins", "--sub-autodetect-fuzzy=0"})
        m_player = m_factory.CreatePlayer (Of IDiskPlayer)()
        AddHandler m_player.Events.PlayerPositionChanged, AddressOf Events_PlayerPositionChanged
        AddHandler m_player.Events.TimeChanged, AddressOf Events_TimeChanged
        AddHandler m_player.Events.MediaEnded, AddressOf Events_MediaEnded
        AddHandler m_player.Events.PlayerStopped, AddressOf Events_PlayerStopped
        m_player.WindowHandle = WindowH
        playercontrol = PControl
    End Sub

    Private Sub Events_PlayerStopped (sender As Object, e As EventArgs)
        InitControls()
    End Sub

    Private Sub Events_MediaEnded (sender As Object, e As EventArgs)
        InitControls()
    End Sub

    Private Sub InitControls()
        Playercontrol.InitControls()
    End Sub

    Public Sub Play()
        m_media = m_factory.CreateMedia (Of IMedia) (MediaPath)
        AddHandler m_media.Events.DurationChanged, AddressOf Events_DurationChanged
        AddHandler m_media.Events.StateChanged, AddressOf Events_StateChanged
        AddHandler m_media.Events.ParsedChanged, AddressOf Events_ParsedChanged
        m_player.Open (m_media)
        m_media.Parse (True)
        m_player.Play()
        m_player.SetSubtitleFile(Nothing)
    End Sub

    Public Sub [Stop]()
        m_player.[Stop]()
    End Sub

    Public Sub Pause()
        m_player.Pause()
    End Sub

    Public Sub ToggleMute()
        m_player.ToggleMute()
    End Sub

    Private Sub Events_TimeChanged (sender As Object, e As MediaPlayerTimeChanged)
        Playercontrol.Events_TimeChanged (sender, e)
    End Sub

    Private Sub Events_PlayerPositionChanged (sender As Object, e As MediaPlayerPositionChanged)
        Playercontrol.Events_PlayerPositionChanged (sender, e)
    End Sub

    Private Sub Events_StateChanged (sender As Object, e As MediaStateChange)
        Playercontrol.Events_StateChanged (sender, e)
    End Sub

    Private Sub Events_DurationChanged (sender As Object, e As MediaDurationChange)
        Playercontrol.Events_DurationChanged (sender, e)
    End Sub

    Private Sub Events_ParsedChanged (sender As Object, e As MediaParseChange)
        Debug.WriteLine (e.Parsed)
    End Sub

    Public Sub TagOpen()
        _mediaTag = TagLib.File.Create(_mediaPath)
    End Sub

    Public Function TagTitle()
        Return (_mediaTag.Tag.Title)
    End Function

    Public Function TagArtist()
        Return (_mediaTag.Tag.FirstPerformer)
    End Function
End Class
